Basic Bar Graphs

library(gcookbook)
library(ggplot2)
library(plotly)
data(pg_mean)

g<-ggplot(pg_mean,aes(x=group,y=weight))+geom_bar(stat="identity",fill="yellow",color="black")+
  ggtitle("Barplot of Group and Weight")
ggplotly(g)

Grouping Bars Together

library(gcookbook)
library(ggplot2)
library(plotly)
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",position="dodge") + xlab("Date")+ ylab("Weight")+ggtitle("Group Barplot of Date and Weight")
ggplotly(g)

Set the colors

  1. Use scale_fill_brewer()

  2. Use scale_fill_manual()

library(gcookbook)
library(ggplot2)
library(plotly)
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",position="dodge",
color="black")+scale_fill_brewer(palette="Dark2") + xlab("Date")+ ylab("Weight")+ggtitle("Group Barplot of Date and Weight")
ggplotly(g)

Making a bar graph of counts

library(datasets)
library(ggplot2)
library(plotly)
data(diamonds)

g<-ggplot(diamonds,aes(x=cut,fill=cut))+ geom_bar()+xlab("Cut")+ylab("Count")+
  ggtitle("Barplot showing count/frequency of cut ")
ggplotly(g)

Continuous variable resulting in a histogram instead of a bar graph

library(datasets)
library(ggplot2)
library(plotly)
data(diamonds)

g<-ggplot(diamonds,aes(x=carat))+ geom_bar(stat="bin")+xlab("Carat")+ylab("Count")+
  ggtitle("Barplot showing count/frequency of Carat ")
ggplotly(g)

Using colors in a bar graph

library(gcookbook)
library(ggplot2)
library(plotly)
data(uspopchange)

upc<-subset(uspopchange,rank(Change)>40)

g<-ggplot(upc,aes(x=Abb,y=Change,fill=Region))+geom_bar(stat="identity")
ggplotly(g)

Set colors manually

library(gcookbook)
library(ggplot2)
library(plotly)
data(uspopchange)

upc<-subset(uspopchange,rank(Change)>40)

g<-ggplot(upc,aes(x=reorder(Abb,Change),y=Change,fill=Region))+geom_bar(stat="identity",
color="black")+scale_fill_manual(values=c("blue","yellow"))+xlab("State")
ggplotly(g)
# you can set the color using scale_fill_manual(values=c("#669933","#FFCC66"))

Coloring Negative and Positive bars differently

library(gcookbook)
library(ggplot2)
library(plotly)
data(climate)

csub<-subset(climate,Source=="Berkeley" & Year>=1900)
csub$pos<-csub$Anomaly10y>=0

g<-ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat="identity",position="identity")+
  scale_fill_manual(values=c("yellow","blue"))+ggtitle("Barplot of Year and Anomaly10y")
ggplotly(g)
library(gcookbook)
library(ggplot2)
library(plotly)
data(climate)

csub<-subset(climate,Source=="Berkeley" & Year>=1900)
csub$pos<-csub$Anomaly10y>=0

g<-ggplot(csub,aes(x=Year,y=Anomaly10y,fill=pos))+geom_bar(stat="identity",position="identity",
color="black",size=0.25)+scale_fill_manual(values=c("#CCEEFF","#FFDDDD"))+ggtitle("Barplot of Year and Anomaly10y")+theme(legend.position="none")
ggplotly(g)

Adjusting Bar Width and Spacing

  1. Set width in geom_bar(). The default value is 0.9. Larger values make the bars wider and smaller values make the bars narrower.

  2. The maximum width is 1.

#Standard width
library(gcookbook)
library(ggplot2)
library(plotly)
library(gridExtra)
data(pg_mean)

g1<-ggplot(pg_mean,aes(x=group,y=weight,fill=group))+geom_bar(stat="identity")+xlab("Group")+
  ylab("Weight")+ggtitle("Barplot of Group and Weight")
ggplotly(g1)
#For narrow bars
g2<-ggplot(pg_mean,aes(x=group,y=weight,fill=group))+geom_bar(stat="identity",width=0.5)+xlab("Group")+ ylab("Weight")+ggtitle("Barplot of Group and Weight")
ggplotly(g2)
#For wider bars
g3<-ggplot(pg_mean,aes(x=group,y=weight,fill=group))+geom_bar(stat="identity",width=1)+xlab("Group")+ ylab("Weight")+ggtitle("Barplot of Group and Weight")
ggplotly(g3)
grid.arrange(g1,g2,g3,ncol=3)

Create Space in grouped bars and some space between the bars

library(gcookbook)
library(ggplot2)
library(plotly)
library(gridExtra)
data(cabbage_exp)

#space between grouped bars
g1<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",width=0.5,
position="dodge")+ggtitle("Barplot of Date and Weight")
ggplotly(g1)
#some space between the bars
g2<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",width=0.5,
position=position_dodge(0.7))+ggtitle("Barplot of Date and Weight")
ggplotly(g2)
#apply gridExtra package
grid.arrange(g1,g2,ncol=2)

Making a stacked bar graph plus the label in the middle of the stack

library(gcookbook)
library(ggplot2)
library(plotly)
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")+xlab("Date")+
  ylab("Weight")+ggtitle("Stacked bar graph of Date and Weight")+geom_text(aes(label=Weight),
                                      size=3,position=position_stack(vjust=0.5))
ggplotly(g)

Reverse the order of the legend

library(gcookbook)
library(ggplot2)
library(plotly)
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")+
guides(fill=guide_legend(reverse=TRUE))+ xlab("Date")+ylab("Weight")+ggtitle("Stacked bar graph of Date and Weight")
ggplotly(g)
  1. There are three levels of Date.

  2. There are two levels of Cultivar.

To reverse the stacking order

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar,order=desc(Cultivar)))+
  geom_bar(stat="identity")+xlab("Date")+ylab("Weight")+ggtitle("Stacked bar graph of Date and Weight")
ggplotly(g)

For a more polished graph

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
data(cabbage_exp)

g<-ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",color="black")+ guides(fill=guide_legend(reverse=TRUE))+scale_fill_brewer(palette="Pastel1")+xlab("Date")+ylab("Weight")+ggtitle("Stacked bar graph of Date and Weight")
ggplotly(g)

Making a proportional stacked bar graph (%age)

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
data(cabbage_exp)

#Do a group wise transform splitting on "Date".

ce<-ddply(cabbage_exp,"Date",transform,percent_weight=Weight/sum(Weight)*100)

g<-ggplot(ce,aes(x=Date,y=percent_weight,fill=Cultivar))+geom_bar(stat="identity")
ggplotly(g)

Adding labels to a bar graph

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
library(gridExtra)
data(cabbage_exp)

#below the top
g1<-ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat="identity")+
  geom_text(aes(label=Weight),vjust=1.5,color="white")

#above the top
g2<-ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat="identity")+
  geom_text(aes(label=Weight),vjust=-0.2) 

grid.arrange(g1,g2,ncol=2)

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
library(gridExtra)
data(cabbage_exp)

#Adjjust y limits to be a little higher
g1<-ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat="identity")+
geom_text(aes(label=Weight),vjust=-0.2)+ylim(0,max(cabbage_exp$Weight)*1.05)

#map y position slightly above the top of the bar
g2<-ggplot(cabbage_exp,aes(x=interaction(Date,Cultivar),y=Weight))+geom_bar(stat="identity")+
geom_text(aes(y=Weight+0.1,label=Weight))

grid.arrange(g1,g2,ncol=2)

For grouped bar graph

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
library(gridExtra)
data(cabbage_exp)

ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",position="dodge")+
  geom_text(aes(label=Weight),vjust=1.5,color="white",position=position_dodge(0.9),size=4)

Putting labels on stacked bars

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
library(gridExtra)
data(cabbage_exp)

#sort the data
ce<-arrange(cabbage_exp,Date,Cultivar)
#get the cumulative sum
ce<-ddply(ce,"Date",transform,label_y=cumsum(Weight))
ce
  Cultivar Date Weight        sd  n         se label_y
1      c39  d16   3.18 0.9566144 10 0.30250803    3.18
2      c52  d16   2.26 0.4452215 10 0.14079141    5.44
3      c39  d20   2.80 0.2788867 10 0.08819171    2.80
4      c52  d20   3.11 0.7908505 10 0.25008887    5.91
5      c39  d21   2.74 0.9834181 10 0.31098410    2.74
6      c52  d21   1.47 0.2110819 10 0.06674995    4.21
g1<-ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")+geom_text(aes(y=label_y,
    label=Weight),vjust=1.5,color="white")

#placing the label in the middle of each stacked bar
ce<-arrange(cabbage_exp,Date,Cultivar)

#calculate y position placing it in the middle
ce<-ddply(ce,"Date",transform,label_y=cumsum(Weight)-0.5*Weight)

g2<-ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity")+
  geom_text(aes(y=label_y,label=Weight),color="white")

grid.arrange(g1,g2,ncol=2)

For a more polished graph

library(gcookbook)
library(ggplot2)
library(plotly)
library(plyr) # needed for desc()
library(gridExtra)
data(cabbage_exp)

#placing the label in the middle of each stacked bar
ce<-arrange(cabbage_exp,Date,Cultivar)

#calculate y position placing it in the middle
ce<-ddply(ce,"Date",transform,label_y=cumsum(Weight)-0.5*Weight)


ggplot(ce,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(stat="identity",color="black")+
  geom_text(aes(y=label_y,label=paste(format(Weight,nsmall=2),"kg")),size=4)+
  guides(fill=guide_legend(reverse=TRUE))+scale_fill_brewer(palette="Pastel1")

Add data label to simple bargraph

library(datasets)
library(ggplot2)
library(plotly)
data(diamonds)


g<-ggplot(diamonds,aes(x=cut,fill=cut))+ geom_bar()+xlab("Cut")+ylab("Count")+
  ggtitle("Barplot showing count/frequency of cut ")+geom_text(stat="count",aes(label=..count..),
  vjust=-0.2)
ggplotly(g)